#include "SchmidtCAM.h"

int main( int argc, char *argv[] ){

    if( argc != 2 ){
        printf("> usage: %s reg_addr\n", argv[0]);
        return 0;
    }
    
    uint32_t reg_addr;
    reg_addr = (uint32_t)(atoi(argv[1]));

    int fd = open("/dev/mem", O_RDWR | O_SYNC);
    uint *ptr1 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_OUT);
    uint *gpio_reg      = ptr1;     // 0x41200000
    uint *gpio2_reg     = ptr1+2;   // 0x41200008
    
    uint *ptr2 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_IN);
    uint *gpio_in_reg   = ptr2;     // 0x41210000

    // SPI config
    SPI_CONFIG *spi_config = (SPI_CONFIG*)malloc(sizeof(SPI_CONFIG));

    setup_SPI_by_device_name( spi_config, "ad9508" );

    AD9508_Config *ad9508_cfg = (AD9508_Config*)malloc(sizeof(AD9508_Config));
    init_ad9508_config( ad9508_cfg );

    // exit(0);

    read_a_ad9508_reg_via_SPI(  gpio_reg,
                                gpio2_reg,
                                gpio_in_reg,
                                spi_config,
                                ad9508_cfg,
                                reg_addr );

    free(ad9508_cfg);
    free(spi_config);

    munmap(ptr1,10);
    munmap(ptr2,10);
    close(fd);

    return 0;
}
